<?php

namespace Frontend\Controllers;

use Models\Doctypes;
use Models\Topics;
use Models\References;

class TopicController extends ControllerBase {

    public function indexAction() {
        
    }

    public function searchAction($id) {
        $text_search = $this->request->getPost('text_search');
        $currentPage = (int) $this->request->getPost('page');
        if (!$currentPage) {
            $currentPage = 1;
        }
        $limit = (int) $this->request->getPost('limit');
        if (!$limit) {
            $limit = 10;
        }
        $limit_days = References::findFirstById('limit_in_day');
        if ($limit_days) {
            $limit_in_day = $limit_days->value;
        } else {
            $limit_in_day = 1;
        }
        $topic = Topics::findFirstById($id);
        $type = Doctypes::findFirstById($topic->doctype_id);
        $topics = Topics::find("doctype_id = '$topic->doctype_id'");
        $query = $this->modelsManager->createBuilder()
                ->columns(array(
                    'Models\Documents.id',
                    'Models\Documents.title',
                    'Models\Documents.description',
                    'Models\Documents.submit_date',
                    'Models\Documents.cost',
                    'Models\Documents.link',
                    'Models\Documents.image',
                    'Models\Documents.topic_id',
                    'Models\Documents.download',
                    'Models\Documents.discount',
                    'Topics.name TopicName',
                ))
                ->from('Models\Documents')
                ->leftjoin('Models\Topics', 'Topics.id = Models\Documents.topic_id', 'Topics')
                ->where("topic_id  = '$id'")
                ->andwhere('Models\Documents.public = 1');

        $docs_in_day = $query->leftjoin('Models\Downloads', 'Downloads.doc_id = Models\Documents.id', 'Downloads')
                        ->groupby('Models\Documents.id')
                        ->orderby('Models\Documents.download DESC')
                        ->limit($limit_in_day)
                        ->getQuery()->execute();

         $query1 = $this->modelsManager->createBuilder()
                ->columns(array(
                    'Models\Documents.id',
                    'Models\Documents.title',
                    'Models\Documents.description',
                    'Models\Documents.submit_date',
                    'Models\Documents.cost',
                    'Models\Documents.link',
                    'Models\Documents.image',
                    'Models\Documents.topic_id',
                    'Models\Documents.download',
                    'Models\Documents.discount',
                    'Topics.name TopicName',
                ))
                ->from('Models\Documents')
                ->leftjoin('Models\Topics', 'Topics.id = Models\Documents.topic_id', 'Topics')
                ->where("topic_id  = '$id'")
                ->andwhere('Models\Documents.public = 1');
        if ($this->request->getPost('text_search')) {
            $query1->andWhere("title  LIKE '%$text_search%' OR description  LIKE '%$text_search%'");
        }
        $docs = $query1->getQuery()->execute();

        $pagintion = new \Phalcon\Paginator\Adapter\Model(
                array(
            "data" => $docs,
            "limit" => $limit,
            "page" => $currentPage
                )
        );
        $this->view->title = 'VIDS - ' . $type->name . ' - ' . $topic->name;
        $this->view->type = $type;
        $this->view->topics = $topics;
        $this->view->topic = $topic;
        $this->view->docs = $pagintion->getPaginate();
        $this->view->text_search = $text_search;
        $this->view->page = $currentPage;
        $this->view->limit = $limit;
        $this->view->docs_in_day = $docs_in_day;
    }

}
